package Service;



import Dao.UserDao;
import Dto.User;
import MyException.MyException;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserService {
    private UserDao userDao = new UserDao();
    public User login(String username, String password) throws SQLException, MyException {
        User user = userDao.login(username);
        if((user != null) && user.getPassword().equals(password)){
            return user;
        }
        throw new MyException("你的用户名密码有错或被禁用");
    }
    //超级用户登录
//    public Super superLogin(String superName, String superPass) throws SQLException, MyException {
//        Super supe = userDao.superLogin(superName);
//        if((supe != null) && supe.getSuperPass().equals(superPass)){
//            return supe;
//        }
//        throw new MyException("你的用户名密码有错");
//    }
    //预注册(判断是否有该用户)
    public User register(String username) throws SQLException, MyException {
        User user = userDao.register(username);
        if (user!=null&&user.getUsername().equals(username)){
            throw new MyException("已有该用户");
        }
        return user;
    }
    //注册
    public void addRegister(User user) throws SQLException {
        userDao.addRegister(user);
    }
    //个人中心预修改
    public User preUpdateLogin(String nickname) throws SQLException {
        User user = userDao.preUpdateLogin(nickname);
        return user;
    }
    //个人中心修改
    public void updateLogin(User user) throws SQLException {
        userDao.updateLogin(user);
    }
    //管理员添加用户
    public void addUser(User user) throws SQLException {
        userDao.addUser(user);
    }
    //分页查询信息
    public Map<String,Object> selectUserByPage(Integer pageNum) throws SQLException{
        List<User> list = userDao.selectUserList(pageNum);
        Long totalCount = userDao.selectUserCount();
        Long totalPageNum = totalCount==0?1:(totalCount%10==0?totalCount/10:totalCount/10+1);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("totalCount",totalCount);
        map.put("totalPageNum",totalPageNum);
        return map;
    }
    //启用
    public void updateEnable(Integer id) throws SQLException {
        userDao.updateEnable(id);
    }
    //禁用
    public void updateDisable(Integer id) throws SQLException {
        userDao.updateDisable(id);
    }

}
